This TechNote
discusses how to use ActionScript to find the square root of a number. The
TechNote covers using the Math Object, introduced in Macromedia Flash 5,
and using Macromedia Flash 4 ActionScript to find the square root of an
input number.
In the
Macromedia Flash movie below, the original value is entered into an input
text field by the user. Pressing a button will yield the square root of
the entered number.
To build
the square root calculator using the Math Object:
1
Create an input text field. Give it the variable name
"value". This will be the entry field for the original
value.
2
Create an dynamic text field to hold the result. Give it the
variable name "squareRoot". This will be the display field for the
square root result.
3
Select
the button instance and bring up the Actions panel.
Enter
the following ActionScript code:
on
(release) { squareRoot=Math.sqrt (value) }
Note: This script uses
the Math Object's sqrt
method which returns the square root of a specified number.
To build
the square root calculator using Flash 4:
1
Create an
editable text field. Select the text field's properties and give it
the variable name "value". This will be the entry field for the
original value.
2
Create an
editable text field to hold the result. Select the text field's
properties and give it the variable name "squareRoot". This will be
the display field for the square root result.
3
Double-click the button instance to access the Instance
Properties dialog.
Select
the Actions tab. Enter the following ActionScript code:
On (Release)
Set Variable: "i" = "0"
Set Variable: "x" = "1"
Loop While (i < 20)
Set Variable: "x" = x-((x*x-value)/(2*x))
Set Variable: "i" = i+1
End Loop
Set Variable: "squareRoot" = x
End On
Note: This script uses
the variables 'value' and 'squareRoot' from steps 1 and 2, which have been
colorized for clarity. Be certain all of the "Value" fields in Set
Variable actions are set to "Expression".
Additional
information For more detail on
working with editable text fields, please refer to Using
type blocks and text fields in Flash (TechNote 14154). For other math
functions in Flash, try John Croteau's "Trig Functions" article.